package com.example.fragment;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.shop.Commodity;
import com.example.shop.R;
import com.example.shop.InfActivity;
import com.example.shop.addCommodity;
import com.example.utils.CommodityDataUtils;

import java.util.ArrayList;

public class

FragmentCommodity extends Fragment {

    View view;
    ListView listView;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        // 为fragment 加载 view：R.layout.fragment_commodity

        view = inflater.inflate(R.layout.fragment_commodity, null);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {

        super.onActivityCreated(savedInstanceState);


        if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            //没有授权进行权限申请
            ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
        }
        if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            //没有授权进行权限申请
            ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_CALENDAR}, 1);
        }
        CommodityDataUtils commodityDataUtils = CommodityDataUtils.getInstance(getContext());
        ArrayList data = commodityDataUtils.getAllCommodity();
        MyAdapter myAdapter = new MyAdapter(data);

        Button bt_add = view.findViewById(R.id.addItem);
        bt_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getContext(), addCommodity.class);
                startActivity(intent);
                getActivity().finish();
            }
        });

        listView = view.findViewById(R.id.listView);
        listView.setAdapter(myAdapter);
        //标题
        TextView tv_main_title = view.findViewById(R.id.tv_main_title);
        tv_main_title.setText("二手商品主页");
        //返回键
        ImageView iv_back = view.findViewById(R.id.iv_back);
        //标题布局
        RelativeLayout rl_title_bar = view.findViewById(R.id.title_bar);
        rl_title_bar.setBackgroundColor(Color.rgb(255, 165, 0));
        //返回键的点击事件
        iv_back.setVisibility(View.GONE);
    }

    class MyAdapter extends BaseAdapter {
        ArrayList<Commodity> data;
        public MyAdapter(ArrayList data) {
            this.data = data;
        }

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return data.get(position).item_id;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {


            if (convertView != null) {
                return convertView;
            }
            convertView = View.inflate(getContext(), R.layout.item_fragment1, null);
            TextView titleItemFragment1;
            TextView pubmanItemFragment1;
            TextView pubtimeItemFragment1;
            ImageView ima;

            titleItemFragment1 = (TextView) convertView.findViewById(R.id.title_item_fragment1);
            pubmanItemFragment1 = (TextView) convertView.findViewById(R.id.pubman_item_fragment1);
            pubtimeItemFragment1 = (TextView) convertView.findViewById(R.id.pubtime_item_fragment1);
            ima = (ImageView) convertView.findViewById(R.id.ima);

            //给每个listview 加载数据
            titleItemFragment1.setText(data.get(position).item_name);
            pubmanItemFragment1.setText(data.get(position).item_profile);
            pubtimeItemFragment1.setText(String.format("¥%s", data.get(position).item_price));
            ima.setImageURI(Uri.parse(data.get(position).item_image));
//            ima.setImageResource(im[position]);

            convertView.setOnClickListener(v -> {
                Intent intent = new Intent();
                intent.putExtra("name", data.get(position).item_name);
                intent.putExtra("profile", data.get(position).item_profile);
                intent.putExtra("price", data.get(position).item_price);
                intent.putExtra("image", data.get(position).item_image);
                intent.setClass(getContext(), InfActivity.class);
                startActivity(intent);

            });
            return convertView;
        }
    }


}